"""应用1的具体实现 - 修正版本"""
import time
from app_automation.base_app import BaseApp
from utils import common_utils, logger

from ai_modules.volcano_ark_model import VolcanoArkModel
from ai_modules.tencent_hunyuan_model import TencentHunyuanModel

# 配置日志
logger = logger.get_logger("App1")

class App1(BaseApp):
    """应用1自动化类"""
    
    def __init__(self, ocr_processor):
        super().__init__(ocr_processor)

        # 初始化火山方舟模型
        self.volcano_model = VolcanoArkModel(
            api_key="9199f23c-32b1-45b3-9196-7c33da9f5c12",
            model="doubao-seed-1-6-250615"
        )

        # 初始化腾讯混元模型
        self.hunyuan_model = TencentHunyuanModel(
            api_key="sk-ghXoPAlQyF4CE2KuLeN6j1OyLUXijAPcPb8IN1erv0BN5lVD",
            model="hunyuan-turbos-latest",
            max_retries=5,
            retry_delay=1.0
        )

    
    
    def execute(self, device_id: str) -> bool:
        """
        执行App1的自动化流程 - 修正点击操作
        """
        try:
            # 1. 唤醒设备
            self._wake_device(device_id)
            time.sleep(1)
            
            # 2. 确保在首页
            self._go_home(device_id)
            time.sleep(1)
            
            # 3. 截取屏幕
            screenshot_path = self._capture_screen(device_id)
            print("screenshot_path: " + (screenshot_path if screenshot_path is not None else ""))
            if not screenshot_path:
                self.log.error(f"截屏失败: {device_id}")
                return False
                
            # 4. OCR处理
            ocr_data = self._process_ocr(screenshot_path)
            if not ocr_data or not ocr_data[0]:
                self.log.error(f"OCR未识别到有效文本: {device_id}")
                return False

            # -----------------------AI处理-----------------------------
            # -----------------------火山方舟模型调用-----------------------------
            # 示例1: 纯文本查询
            volcano_text = self.volcano_model.text_chat("介绍一下自己")
            print(f"火山方舟响应: {volcano_text[:100]}")  # 只显示前100个字符

            # -----------------------腾讯混元--------------------------------
            hunyuan_text = self.hunyuan_model.text_chat(
                "介绍一下你自己",
                timeout=60.0,
                max_tokens=400
            )
            
            print(f"腾讯混元响应: {hunyuan_text}")












                
            # 5. 定位目标位置
            target_text = "设置"
            texts, boxes, _ = ocr_data
            results = common_utils.locate_text_center(target_text, texts, boxes)
            if not results:
                self.log.warning(f"未找到目标文本: '{target_text}'")
                return False
                
            # 6. 执行点击 - 修正为使用ADBUtils实例
            x, y = results[0]
            # 使用self.adb访问ADBUtils实例
            if self.adb.tap_screen(x, y, device_id):  # 修正后的调用方式
                self.log.info(f"成功点击目标: {target_text} @ ({x}, {y})")
                return True
            else:
                self.log.error(f"点击目标失败: {target_text} @ ({x}, {y})")
                return False
            
        except Exception as e:
            self.log.error(f"App1执行异常: {str(e)}")
            return False